home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / ncsat.cpt / Telnet2.5 final / tcpip / macutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-03  |  11.2 KB  |  553 lines

  1. /*  MACUTIL.C
  2. *   Utilities for the network library that are Macintosh specific
  3. ****************************************************************************
  4. *                                                                          *
  5. *      part of:                                                            *
  6. *      TCP/UDP/ICMP/IP Network kernel for NCSA Telnet                      *
  7. *      by Tim Krauskopf                                                    *
  8. *                                                                          *
  9. *      National Center for Supercomputing Applications                     *
  10. *      152 Computing Applications Building                                 *
  11. *      605 E. Springfield Ave.                                             *
  12. *      Champaign, IL  61820                                                *
  13. *                                                                          *
  14. *                                                                          *
  15. ****************************************************************************
  16. */
  17. #include <stdio.h>
  18. #include <string.h>
  19.  
  20. #ifdef UNDERNOCIRCUMSTANCES
  21. #include "protocol.h"
  22. #include "data.h"
  23. #endif
  24.  
  25. #ifdef MPW
  26. #include "mpw.h"
  27. #endif
  28.  
  29. #include <Dialogs.h>
  30. #include <Files.h>
  31. #include <Types.h>
  32. #include <AppleTalk.h>
  33. #include <Events.h>
  34. #include <Errors.h>
  35. #include <OSutils.h>
  36. #include <Memory.h>
  37. #include <Strings.h>
  38.  
  39. #include "configrec.h"
  40. #include "maclook.h"
  41.  
  42. /* Some globals for file lookup */
  43. int defaultv;
  44.  
  45. ParamBlockRec FLpb;
  46. CInfoPBRec HFLpb, tHFLpb;
  47. char FileName[256], FileTemplate[256];
  48. extern int * DirTree;
  49. char *iobufptr=0L,*iobuf=0L,*iobufmax=0L;
  50. int iobufsize,lastcr;
  51.  
  52. int getWDnum()
  53.   /* returns reference of current default volume or working directory */
  54.   {
  55.     ParamBlockRec pb;
  56.  
  57.     pb.ioParam.ioNamePtr = 0L;
  58.     PBGetVol(&pb, FALSE);    /* see Tech Note 140 for implications of this */
  59.     return(pb.ioParam.ioVRefNum);
  60.   }
  61.  
  62. Str255 *getVname()
  63. {
  64.     WDPBRec pb;
  65.     char *temp;
  66.  
  67.     temp=NewPtr(256);
  68.  
  69.     pb.ioNamePtr=temp;
  70.     PBGetVol((ParmBlkPtr) &pb,FALSE);
  71.     (*temp)++;
  72.     temp[*temp]=':';
  73.     temp[*temp+1]=0;
  74.     return        /* This proc returns the Vol reference */
  75.         (Str255 *) temp;
  76. }
  77.  
  78. Str255 *getWDname()
  79. {
  80.     WDPBRec pb;
  81.     CInfoPBRec cpb;
  82.     char tempst[100],*wdtemp,*wdout,*start,*store,*trav;
  83.     int i,j;
  84.  
  85.     if (!isHFS()) return(getVname()); /* if not HFS then only volume exists */
  86.  
  87.     wdout=NewPtr(256);
  88.     wdtemp=NewPtr(256);
  89.     setmem(wdout, 256, 0);                    /* BYU 2.4.18 */
  90.  
  91.     sprintf(tempst,"ioNamePtr = %lx",wdout);
  92.     putln (tempst);
  93.  
  94.     pb.ioNamePtr=wdout;
  95.     PBHGetVol(&pb,FALSE);
  96.  
  97. #ifdef WANNACOLON
  98.     (*wdout)++;
  99.     wdout[*wdout]=':';
  100. #endif WANNACOLON
  101.  
  102.     cpb.dirInfo.ioCompletion=0L;
  103.     cpb.dirInfo.ioVRefNum=pb.ioWDVRefNum;
  104.     cpb.dirInfo.ioDrDirID=pb.ioWDDirID;
  105.     trav=wdtemp;
  106.     DirTree[ (j=0) ]=pb.ioVRefNum;
  107.  
  108.     while(cpb.dirInfo.ioDrDirID!=0) {
  109.         cpb.hFileInfo.ioNamePtr=trav;
  110.         cpb.hFileInfo.ioFDirIndex=-1;
  111.         if (PBGetCatInfo(&cpb,FALSE)!=0)  {cpb.dirInfo.ioDrDirID=0; break;}
  112.         if (*trav==0) {cpb.dirInfo.ioDrDirID=0;break;}
  113.         i=*trav; *trav=0; start=trav+1;
  114.         trav+=i+1; *trav=0;
  115.         DirTree[j++]=cpb.dirInfo.ioDrDirID;
  116.         cpb.dirInfo.ioDrDirID=cpb.dirInfo.ioDrParID;
  117.         }
  118.  
  119.     *wdtemp=0;
  120.     store=wdout+*wdout+1;
  121.     if (trav-wdtemp) *wdout=(trav-wdtemp);
  122.     if (trav!=wdtemp) start=trav-1; else start=wdtemp;
  123.  
  124.     if (start!=wdtemp) {
  125.         while(*start) start--;        /* Go to beginning of string */
  126.         if (start!=wdtemp) start--;
  127.         }
  128.  
  129.     while(start!=wdtemp) {
  130.         while(*start) start--;        /* Go to beginning of string */
  131.         trav=start+1; *store='/';    /* Ready to move directory name */
  132.         store++;
  133.         while (*trav) {*store=*trav; store++;trav++; } /* store it */
  134.         if (start!=wdtemp) start --;
  135.         *store=0;
  136.         }
  137.     DisposPtr(wdtemp);
  138.     return((Str255 *) wdout);
  139. }
  140.  
  141. setWDnum( num)
  142. int num;
  143. {
  144.     WDPBRec pb;
  145.  
  146.     pb.ioNamePtr=0L; 
  147.     pb.ioVRefNum=num;
  148.     pb.ioWDDirID=0L;
  149.     PBHSetVol(&pb,FALSE);
  150. }
  151.  
  152. /*
  153. *  setSFdir
  154. *  trick to set up the working directory from the last SFgetfile. TK
  155. */
  156.  
  157. #define SFSaveDisk  0x214
  158. #define CurDirStore 0x398
  159.  
  160. void setSFdir
  161.   (
  162.     void
  163.   )
  164. {
  165.     WDPBRec pb;
  166.  
  167.     if (!isHFS()) {
  168.         defaultv = - (* (short *) SFSaveDisk);
  169.         return;
  170.     }
  171.     
  172.     pb.ioNamePtr=0L; 
  173.     pb.ioWDProcID=0L; 
  174.     pb.ioVRefNum=  - (* (short *) SFSaveDisk);        /* saved from last SF package call */
  175.     pb.ioWDDirID= (* (long *) CurDirStore);            /* BYU 2.4.18 */
  176.     PBOpenWD(&pb,FALSE);
  177.     
  178.     defaultv = pb.ioVRefNum;
  179.     setvol(NULL, defaultv);            /* make working directory */
  180.  
  181. }
  182.  
  183. setWDname( num, name)
  184. int num;
  185. Str255 *name;
  186. {
  187. #pragma unused(name)
  188.     WDPBRec pb;
  189.  
  190.     pb.ioNamePtr=0L; 
  191.     pb.ioVRefNum=num;
  192.     pb.ioWDDirID=0L;
  193.     PBHSetVol(&pb,FALSE);
  194. }
  195.  
  196. int wccheck(file,template)    /* BYU - routine now returns (int) for recursion. */
  197. char *template,*file;
  198. {
  199.     while(*template) {
  200.         if (*template=='*') {
  201.             template++;
  202.             if (*template) {
  203.                 while((*file) && !wccheck(file,template)) file++;    /* BYU */
  204.                 if ((*file)==0) return(0);
  205.                 }
  206.             else return(1);
  207.             continue;
  208.             }
  209.         else
  210.             if ((*template!='?') && (*template!=*file)) return(0);
  211.         template++;file++;
  212.         }
  213.     if (*file)            /* BYU */
  214.         return(0);        /* BYU */
  215.     else                /* BYU */
  216.         return(1);        /* BYU */
  217. }
  218.  
  219. /**********************************************************************/
  220. /*
  221. *   Find directory name -- return a code that indicates whether the
  222. *   directory exists or not.
  223. *   0 = dir name ok
  224. *   -1 = error
  225. *   > 0 = dos error code, no dir by this name
  226. *
  227. *   Accept certain unix conventions, like '/' for separator
  228. *
  229. *   Also, append a '\' to the name before returning
  230. *  Note:  There must be enough room in the string to append the '\'
  231. */
  232.  
  233. direxist(dirname)
  234.     char dirname[];
  235.     {
  236. #pragma unused(dirname)
  237.     return(-1);
  238.     }
  239.  
  240. /**********************************************************************/
  241. /* firstname
  242. *  find the first name in the given directory which matches the wildcard
  243. *  specification
  244. *
  245. *  must NewPtr enough space for the path plus a full filename
  246. *
  247. *  expand '*' (unix) to '*.*' (dos)
  248. */
  249. char *firstname(spec)
  250. char *spec;
  251. {
  252.     char *loader;
  253.     int HFS;
  254.  
  255.     setvol(0L, defaultv);                            /* Go to default Directory */
  256.  
  257.     if ( (HFS=isHFS()) )
  258.         PBHGetVol((WDPBPtr) &HFLpb, FALSE);
  259.     else
  260.          PBGetVol(&FLpb,FALSE);
  261.  
  262.     loader=FileTemplate;
  263.     while(*loader++=*spec++);
  264.     putln(spec);
  265.     putln(FileTemplate);
  266.     if ( HFS) {
  267.         HFLpb.hFileInfo.ioCompletion=0;
  268.         HFLpb.hFileInfo.ioNamePtr=FileName;
  269.         HFLpb.hFileInfo.ioFVersNum=0;
  270.         HFLpb.hFileInfo.ioFDirIndex=1;
  271.         tHFLpb=HFLpb;
  272.         if (PBGetCatInfo(&HFLpb,FALSE)!=0)  return(0L);
  273.         }
  274.     else {
  275.         FLpb.fileParam.ioCompletion=0;
  276.         FLpb.fileParam.ioNamePtr=FileName;
  277.         FLpb.fileParam.ioFVersNum=0;
  278.         FLpb.fileParam.ioFDirIndex=1;
  279.         if(PBGetFInfo(&FLpb,FALSE)!=0) return(0L);
  280.         }
  281.     FileName[FileName[0]+1]=0;
  282.     while(!wccheck(&FileName[1],FileTemplate)) {
  283.         if (HFS) {
  284.             tHFLpb.hFileInfo.ioFDirIndex++;
  285.             HFLpb=tHFLpb;
  286.             if (PBGetCatInfo(&HFLpb,FALSE)!=0)  return(0L);
  287.             }
  288.         else {
  289.             FLpb.fileParam.ioFDirIndex++;
  290.             if(PBGetFInfo(&FLpb,FALSE)!=0) return(0L);
  291.             }
  292.         FileName[FileName[0]+1]=0;
  293.         }
  294.  
  295.     if (isHFS() && (HFLpb.hFileInfo.ioFlAttrib & 16))  {
  296.         FileName[++FileName[0]]='/';
  297.         FileName[FileName[0]+1]=0;
  298.         }
  299.  
  300.     return(&FileName[1]);
  301. }
  302.  
  303. /**********************************************************************/
  304. /* nextname
  305. *  modify the path spec to contain the next file name in the
  306. *  sequence as given by DOS
  307. *
  308. *  if at the end of the sequence, return NULL
  309. */
  310. char *nextname()
  311. {
  312.     if (isHFS() ) {
  313.         tHFLpb.hFileInfo.ioFDirIndex++;
  314.         HFLpb=tHFLpb;
  315.         if (HFLpb.hFileInfo.ioNamePtr!=FileName) return(0L);
  316.         if (PBGetCatInfo(&HFLpb,FALSE)!=0)  return(0L);
  317.         FileName[FileName[0]+1]=0;
  318.         while(!wccheck(&FileName[1],FileTemplate)) {
  319.             putln(&FileName[1]);
  320.             tHFLpb.hFileInfo.ioFDirIndex++;
  321.             HFLpb=tHFLpb;
  322.             if (PBGetCatInfo(&HFLpb,FALSE)!=0)  return(0L);
  323.             FileName[FileName[0]+1]=0;
  324.             }
  325.         }
  326.     else {
  327.         if (FLpb.fileParam.ioNamePtr!=FileName) return(0L);
  328.         FLpb.fileParam.ioFDirIndex++;
  329.         if(PBGetFInfo(&FLpb,FALSE)!=0) return(0L);
  330.         FileName[FileName[0]+1]=0;
  331.         while(!wccheck(&FileName[1],FileTemplate)) {
  332.             FLpb.fileParam.ioFDirIndex++;
  333.             if(PBGetFInfo(&FLpb,FALSE)!=0) return(0L);
  334.             FileName[FileName[0]+1]=0;
  335.             }
  336.         }
  337.  
  338.     if (isHFS() && (HFLpb.hFileInfo.ioFlAttrib & 16))  {
  339.         FileName[++FileName[0]]='/';
  340.         FileName[FileName[0]+1]=0;
  341.         }
  342.  
  343.     return(&FileName[1]);
  344. }
  345.  
  346.  
  347. /************************************************************************/
  348. /* userabort
  349. *  check to see if the user wants to abort
  350. *  For the PC, just see if the user has pressed ESC
  351. *  return true if the user wants to abort
  352. */
  353. int userabort
  354.   (
  355.     void
  356.   )
  357. {
  358.     EventRecord theEvent;
  359.  
  360.     while (GetNextEvent(24,&theEvent)) {
  361.         if ((theEvent.modifiers & cmdKey) &&
  362.             (theEvent.message & 0xff) =='.') 
  363.             return(-1);
  364.         }
  365.     return(0);
  366. }
  367.  
  368. void dopwd
  369.   (
  370.     char *where,
  371.     int howmuch
  372.   )
  373. {
  374.     char *temp;
  375.     int i;
  376.  
  377.     if (howmuch<255) return/*(-1)*/;
  378.     setvol(0L, defaultv);                            /* Go to default Directory */
  379.     temp = (char *) getWDname();
  380.     where[0]='/';
  381.     for( i=1;i<=temp[0];i++) where[i]=temp[i];
  382.     where[temp[0]]=0;
  383.     putln(&temp[1]);
  384.     DisposPtr(temp);
  385.     putln(where);
  386. }
  387.  
  388.  
  389. int chgdir
  390.   (
  391.     char *modifier
  392.   )
  393. {
  394.     WDPBRec pb;
  395.     char tempst[256], *nSlash, *start;
  396.     int i=0, j=0;
  397.  
  398.     if (! isHFS()) return(-1);
  399.  
  400.     setvol(0L, defaultv);                            /* Go to default Directory */
  401.  
  402.     start = (char *) getWDname();
  403.     DisposPtr(start);
  404.  
  405.     start=modifier;
  406.     tempst[0]=':';
  407.  
  408.     putln(start);
  409.  
  410.     if ( strcmp("..",modifier)==0) {
  411.         modifier[0]=0;
  412.         j++;
  413.         }
  414.     else
  415.     while ( (nSlash = index(modifier,'/') ) !=0L) {
  416.         if (( (nSlash -modifier) ==2) && (*modifier=='.') && (*(modifier+1) =='.') ) {
  417.             modifier +=3L;
  418.             start=modifier;
  419.             j++;
  420.             }
  421.         else {
  422.             *nSlash=':';
  423.             modifier=nSlash+1L;
  424.             }
  425.         }
  426.  
  427.     if ( *start==':') {
  428.         strcpy( tempst, start+1L);
  429.         j= strlen( tempst);
  430.         if (tempst[j-1] !=':') {
  431.             tempst[j] = ':';
  432.             tempst[j+1]='\000';
  433.             }
  434.         }
  435.     else
  436.         strcpy( tempst+1L, start);
  437.  
  438.     putln(tempst);
  439.  
  440.     pb.ioNamePtr=ctop(tempst);
  441. #ifdef MPW
  442.     c2pstr(tempst);
  443. #endif
  444.     pb.ioVRefNum=0;
  445.     pb.ioWDDirID=DirTree[j];
  446.     pb.ioCompletion=0L;
  447.     pb.ioWDProcID='NCSA';
  448.  
  449.     j = PBOpenWD( &pb,0);
  450.  
  451.     if ( (j==-35) || (j==-43) ) return(1);
  452.  
  453. #ifdef OLDM
  454.     setvol(0L, (defaultv=getWDnum()) );
  455. #endif
  456.  
  457.     j = setvol(0L, (defaultv=pb.ioVRefNum));
  458.  
  459.     if ( (j==-35) || (j==-43) ) return(1);
  460.  
  461.     return(0);
  462. }
  463.  
  464. /*
  465.  *
  466.  */
  467.  
  468. int Scolorset
  469.   (
  470.     unsigned int *ip,
  471.     char *s
  472.   )
  473. {
  474.     if (3==sscanf(s,"{%d,%d,%d}", &ip[0], &ip[1],&ip[2]) )
  475.         return(1);
  476.     return(0);
  477. }
  478.  
  479. long time(x)
  480. long x;
  481. {
  482. #pragma unused(x)
  483.     return( TickCount()/60);
  484. }
  485.  
  486. int memcmp( a, b, len)
  487. char *a, *b;
  488. int len;
  489. {
  490.     while (len --)
  491.         if (*a!=*b) 
  492.             return(1);
  493.     return(0);
  494. }
  495.  
  496. /**************************************************************************/
  497. /*  defdir and sysdir
  498. *   Use to change to and from the system folder directory.
  499. */
  500. #define CurrentVersion 1            /* Last known SysEnvirons version */
  501.  
  502. void sysdir
  503.   (
  504.     void
  505.   )
  506. {
  507.     int err;
  508.     char s[50];
  509.     SysEnvRec theWorld;                /* System Environment record */
  510.     
  511.     err = SysEnvirons(CurrentVersion, &theWorld);
  512.     if (err == envVersTooBig) {
  513.         /* should post a message saying we need to be updated */
  514.         putln("SysEnvirons out of date - macutil.c");
  515.     }
  516.     if (err != noErr)                /* possibly not available assume nothing here! */
  517.         return;
  518.  
  519.     (void) setvol(NULL, theWorld.sysVRefNum);    /* change default volume */
  520.     sprintf(s,"set sysdir: %d",theWorld.sysVRefNum);
  521.     putln(s);
  522.     
  523. }
  524.  
  525. int setmydir
  526.   (
  527.     void
  528.   )
  529. {
  530.     char s[50];
  531.     
  532.     (void) setvol(NULL, defaultv);
  533.     sprintf(s,"set defdir: %d",defaultv);
  534.     putln(s);
  535.     return(defaultv);
  536. }
  537.  
  538. int makemydir
  539.   (
  540.     int dir
  541.   )
  542. {
  543.     char s[50];
  544.     
  545.     defaultv = dir;
  546.     setvol(NULL, defaultv);
  547.     sprintf(s,"make dir: %d",defaultv);
  548.     putln(s);
  549.     return(defaultv);
  550.     
  551. }
  552.